package org.real.guetshop.service.impl;



import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.real.guetshop.context.BaseContext;
import org.real.guetshop.mapper.AddressMapper;
import org.real.guetshop.pojo.dto.AddressDTO;
import org.real.guetshop.pojo.entity.Address;
import org.real.guetshop.pojo.vo.AddressVO;
import org.real.guetshop.service.AddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressMapper addressMapper;

    /**
     * 用户添加收货地址
     * @param addressDTO
     */
    @Override
    public void addNewAddress(AddressDTO addressDTO) {
        addressDTO.setUserId(BaseContext.getCurrentId());
        addressMapper.addNewAddr(addressDTO);
    }

    /**
     * 用户获取收货地址列表
     * @return
     */
    @Override
    public List<AddressVO> getAddrById() {
        return addressMapper.getAddrById(BaseContext.getCurrentId());
    }

    /**
     * 用户设置默认地址
     * @param addressDTO
     */
    @Override
    public void setDefaultAddr(AddressDTO addressDTO) {
        Address address = setAddr(addressDTO);
        addressMapper.setDefaultAddr(address);
    }

    /**
     * 用户删除地址
     * @param addressDTO
     */
    @Override
    public void deleteAddr(AddressDTO addressDTO) {
        Address address = setAddr(addressDTO);
        addressMapper.deleteAddr(address);
    }

    /**
     * 用户编辑收货地址
     * @param addressDTO
     */
    @Override
    public void editAddr(AddressDTO addressDTO) {
        Address address = setAddr(addressDTO);
        addressMapper.editAddr(address);
    }

    public Address setAddr(AddressDTO addressDTO) {
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO, address);
        address.setUserId(BaseContext.getCurrentId());
        return address;
    }
}
